<template>
  <div>

    <!-- 搜索框 -->
    <el-row>
      <el-col :span="5">
        <el-select v-model="selectValue" placeholder="请选择搜索方式">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="10">
        <el-input v-if="selectValue=='选项1'||selectValue=='选项2'" v-model="searchContent" :placeholder="searchPlaceWord" prefix-icon="el-icon-search"></el-input>
        <div class="block" v-else-if="selectValue=='选项3'">
          <el-date-picker
            v-model="searchDate"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            value-format="yyyy-MM-dd hh:mm:ss">
          </el-date-picker>
        </div>
      </el-col>
      <el-col :span="9">
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-col>
    </el-row>

    <div style="margin-top: 20px">
      
      <!-- 考试信息表 -->
      <el-table style="width: 100%" border :data="list"
        :row-class-name="tableRowClassName"
        :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
        ref="filterTable"
      >
        <el-table-column prop="name" label="考试名称" width="width">
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="100">
        </el-table-column>
        <el-table-column prop="prop" label="人脸需求" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isFaceCheck? 'success' : 'danger'"
              disable-transitions>{{scope.row.isFaceCheck?'√':'×'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="提前交卷" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isAdvance? 'success' : 'danger'"
              disable-transitions>{{scope.row.isAdvance?'√':'×'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="复制粘贴" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isCopyPaste? 'success' : 'danger'"
              disable-transitions>{{scope.row.isCopyPaste?'√':'×'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="允许进入考试" width="75" align="center">
          <template slot-scope="scope">
            <el-tag
              type="warning"
              disable-transitions>{{scope.row.timeLimit}} min
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastTime" label="总时长" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              type="primary"
              disable-transitions>{{scope.row.lastTime}} min</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="100" align="center">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="100" align="center">
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="90" align="center">
          <template slot-scope="{row,$index}">
            <el-tooltip content="成绩统计" placement="top-end">
              <el-button icon="el-icon-caret-right" type="primary" size="mini" circle @click="enterOldTestPaper(row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
          style="margin-top: 20px; text-align: center"
          :current-page.sync="page"
          :total="total"
          :page-size="limit"
          :page-count="7"
          :page-sizes="[3, 5, 10]"
          @current-change="getPageList"
          @size-change="handleSizeChange"
          layout="prev, pager, next, jumper, ->, sizes, total"
        >
      </el-pagination>

    </div>

  </div>
</template>

<script>
export default {
  name:'gradeStatistics',
  data() {
    return {
      //当前看中状态
      activeName: '3',
      //真列表数据
      list:[],
      //代表的分页器第几页
      page:1,
      //当前页数展示数据条数
      limit:5,
      //总共数据条数
      total:0,
      //exambody条件
      examForm:{
        id:'',
        //试卷id
        paperId:'',
        //考试名称
        name:'',
        //备注
        remark:'',
        //是否在考试中开启人脸认证
        isFaceCheck:'',
        //限时进入（分钟）
        timeLimit:'',
        //是否可以提前交卷
        isAdvance:'',
        //是否支持复制黏贴
        isCopyPaste:'',
        //开始时间
        startTime:'',
        //结束时间
        endTime:'',
        //持续时间（分钟）
        lastTime:'',
        //创建时间
        gmtCreate:'',
        //修改时间
        gmtModified:'',
      },
      //关键词搜索考试
      searchContent:'',
      //用于搜索
      options: [{
        value: '选项1',
        label: '考试名称'
      }, {
        value: '选项2',
        label: '考试备注'
      }, {
        value: '选项3',
        label: '日期'
      }, ],
      //下拉框默认值
      selectValue: '选项1',
      //输入框占位符内容
      searchPlaceWord:'请输入考试名称',
      // 快捷搜索日期选项
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 搜索的日期内容
      searchDate: '',
    };
  },
  //组件挂载完毕发请求
  mounted() {
    //获取列表数据方法
    this.getPageList();
  },
  methods: {
    // 进入单场考试的成绩统计界面 
    enterOldTestPaper(row){
      this.$router.push({
          name:'gradeOverall',
          query:{
            exam_info:JSON.stringify(row)
          },
      })
    },
    //搜索
    search(){
      //先判断一下有无搜索内容
      //根据搜索类型，赋值
      if(this.selectValue=='选项1'){
        this.examForm.name = this.searchContent;
      }else if(this.selectValue=='选项2'){
        this.examForm.remark = this.searchContent;
      }else if(this.selectValue=='选项3'){
        if(this.searchDate){
          this.examForm.startTime = this.searchDate[0];
          this.examForm.endTime = this.searchDate[1];
        }else{
          this.examForm.startTime = '';
          this.examForm.endTime = '';
        }
      }
      //重新调用获取列表函数
      this.getPageList();
    },
    tableRowClassName({row, rowIndex}) {
      // 实现表格的自定义斑马纹
      // if (rowIndex%2 === 0) {
      //   return 'warning-row';
      // } else if (rowIndex%2 === 1) {
      //   return 'success-row';
      // }
      if (rowIndex%2 === 1) {
        return 'success-row';
      }
      return '';
    },
    //获取考试列表的数据
    async getPageList(pager = 1) {
      this.page = pager;
      //获取考试列表的接口
      let result = await this.$API.exam_paper_management.getExam(this.examForm,this.page,this.limit,parseInt(this.activeName));
      if(result.code==20000){
        this.list = result.data.exams;
        this.total = result.data.total;
      }
    },
    //当分页器某一页需要展示数据条数变化时触发
    handleSizeChange(limit){
      this.limit = limit;
      this.getPageList();
    },
  },
  watch:{
    //监听selectValue
    selectValue:{
      handler(newValue,oldValue){
        // 应该会清空输入框数据
        this.searchContent = '';
        // 想要根据下拉列表变化输入框占位符
        if(newValue=='选项1'){
          this.searchPlaceWord = '请输入考试名称';
        }else if(newValue=='选项2'){
          this.searchPlaceWord = '请输入考试备注信息';
        }
      }
    },
  },
}
</script>

<style>
.el-table .success-row {
  background: #F2F6FC;
}
</style>